﻿/*
 * Created by 长春驿枫 on 2015-8-6.
 * 重写 yui组件,自写组件
 */



;!(function(){
    var __functionForUi=function (Ladder) {
	var thirds=[
		Ladder.BasicURL+"/vendor/pinyin.js",
		Ladder.BasicURL+"/third/smartmenu/jquery-smartMenu-min.js",
		Ladder.BasicURL+"/third/smartmenu/smartMenu.css",
		Ladder.BasicURL+"/css/ladder.css"
	]
    Ladder.include(thirds);
    Ladder.NS("YUI.intab");
    Ladder.YUI.intab={
        intabObject:null,
        init:function(el,callback){
            Ladder.YUI.intab.intabObject=new Ladder.InTab(el,false);
            Ladder.YUI.intab.intabObject.handle();
        },
        switchTo:function(finder,type){
            Ladder.YUI.intab.intabObject.switchTo(finder,type);
        }
    };
    Ladder.NS("InTab");
    /**
     * intab
     * @constructor
     */
    Ladder.InTab=function(){
        var el, option,fullWidget;
        for (var i = 0; i < arguments.length; i++) {
            var type = Ladder.getType(arguments[i]);
            if (type === "Object") {
                if (arguments[i] instanceof $) {
                    el = arguments[i];
                } else {
                    option = arguments[i];
                }
            }
            if (type === "Boolean") fullWidget = arguments[i];
            if (type === "String") el = arguments[i];
        }
        if (el != undefined) {
            if (option == undefined) option = new Object();
            option.parentEL = $(el);
        }
        if (option==undefined)return;
        this.settings={
            container:null,
            index:0,
            dubug:1,
            parentEL:null,
            intabEl:null,
            tabsEL:null,
            tabbtns:null,
            fullWidget:true
        };
        this.tabData=new Object();
        this.currentTab=null;

        if (fullWidget != undefined) option.fullWidget = fullWidget;
        if (Ladder.getType(option) != "Object") return;
        this.settings=$.extend(this.settings,option);
        this.init(option);
    };
    Ladder.InTab.prototype={
        init:function(option){
            var me=this;
            //me.settings=$.extend(me.settings,option);
            if(me.settings.fullWidget){
                if(!me.settings.intabEl)me.settings.intabEl=$("<div class='yui-intab'></div>");
                if(!me.settings.tabsEL)me.settings.tabsEL=$('<ul class="yui-intab-tabs"></ul>');
                if(!me.settings.tabbtns)me.settings.tabbtns=$('<div class="tabbtns"><ul style="height: 28px;"></ul><span class="otherView"></span></div>');
                me.settings.intabEl.append(me.settings.tabsEL).append(me.settings.tabbtns);
                me.settings.parentEL.prepend(me.settings.intabEl);
            }else{
                me.settings.intabEl=me.settings.parentEL;
            }
        },
        defaultTabModel:function(){
            var def={
                refresh:false,
                lazyUrl:""
            };
            return def;
        },
        /**
         * 添加一个tab
         * {
         *      id
         *      target:（选择器）目标对象->tab对应的页对象,
         *      refresh：（逻辑值）用于iframe的刷新，
         *      lazyUrl：
         *      onClick: 点击事件
         * }
         * @param option
         */
        addTab:function(option){
            var me=this;
            var type=Ladder.getType(option);
            var def=me.defaultTabModel();
            if(type==="String") def.title=option;
            if(type==="Object")def= $.extend(def,option);
            //2015-10-17,重新命名click事件，进行旧代码兼容
            if(!def.hasOwnProperty("onClick")&&def.hasOwnProperty("click")){
                def.onClick=def.click;
            }
            if(!def.id)def.id="tab"+me.settings.index++;
            var $tab=$("<li id='"+def.id+"'></li>");
            if(def.style)$tab.attr("style",def.style);
            for(var prop in def) {
                if (def.hasOwnProperty(prop)) {
                    if (!/(id)|(title)|(click)|(style)|(on[A-Z][a-z]\w*)/gi.test(prop))
                        $tab.attr("data-" + prop, def[prop]);
                }
            }
            me.tabData[def.id]=def;
            //处理标题的UI
            $tab.append("<s></s><span>"+def.title+"</span><i></i>");
            $tab.click(function(){
                var $this = $(this);//当前tab
                def.clickCount=def.clickCount?++def.clickCount:1;
                if(def.clickCount===1 && option.onFirst){
                    option.onFirst();
                }
                if(option.onClick){
                    var re_t=option.onClick(def);
                    //如果点击事件禁止向下执行
                    if(re_t!=undefined&&re_t===false){
                        return;
                    }
                }
                //处理旧的tab页
                $this.siblings().removeClass("current").each(function(){
                    var oldtarget = $(this).attr("data-target");
                    if(oldtarget)$(oldtarget).hide();
                });
                me.currentTab=$this;
                $this.addClass("current");
                var target = $this.attr("data-target");
                //显示tab关联的按钮
                me.settings.intabEl.find(".tabbtns [data-target]").hide().each(function () {
                    var $btn = $(this);
                    if($btn.data("limit")==0)return;
                    if ($this.attr("id") && $btn.attr("data-target") == "#" + $this.attr("id")) {
                        $btn.show();
                    }
                });
                if(def.clickCount===1 && def.onLoad)def.onLoad(def);
                if(!target||target=='false')return;
                //处理目标对象
                if(target&&$(target).length>0){
                    var $target=$(target).show();
                    //2015-10-17，此处代码是别处移植，算法混乱
                    var _iframe = $target.is("iframe")?$target:$(target).find("iframe");
                    if (_iframe.length > 0 && _iframe.attr("data-refresh")) {
                        var url = _iframe.attr("data-lazy") || _iframe.attr("src");
                        _iframe.attr("src", "");
                        _iframe.attr("src", url);
                    }
                    if (_iframe.length > 0 && _iframe.attr("data-lazy")) {
                        _iframe.attr("src", _iframe.attr("data-lazy"));
                        _iframe.removeAttr("data-lazy");
                    }
                }
            });
            if(option.current){
                $tab.addClass("current");
                me.currentTab=$tab;
                $tab.trigger("click");
            }
            me.settings.tabsEL.append($tab);
            return $tab;
        },
        /**
         *  添加按钮
         * @param option
         * {
         *      css:样式名称
         *      mark:class标记
         *      id:id
         *      title:标题
         *      其他：扩展到按钮的data区
         * }
         */
        addButton:function(option){
            if(!option&&Ladder.getType(option)!="Object")return;
            var me = this;
            var $li = $("<li></li>");
            if (option.css == undefined) option.css = "btn-default "+(option.mark!=undefined?option.mark:"");
            var $button = $("<button class='" + option.css + "'" + (option.id != undefined ? "id='" + option.id + "'" : "") + ">" + option.title + "</button>");
            delete option.id;
            delete option.title;
            delete option.mark;
            delete option.css;
            if (option.click != undefined&&!option.onClick) {
                option.onClick=option.click;
            }
            if(option.onClick)$button.click(function () { option.onClick() });
            delete option.click;
            if(option.style!=undefined)$button.attr("style",option.style);
            delete option.style;
            for (var prop in option){
                if(!/(click)|(style)/gi.test(prop))
                $button.attr("data-" + prop, option[prop]);
            }
            $li.append($button);
            me.settings.tabbtns.find("ul").append($li)
        },

        addOtherView:function(html){
            var $li = $("<li></li>").append(html);

            this.settings.tabbtns.find("ul").append($li)
        },
        /**
         * 处理intab过程
         * @param el
         */
        handle: function (el) {
            var me=this;
            if(!el)el=me.settings.intabEl;
            el = $(el);
            if (el.length < 1)return;
            //添加监听
            el.find(".yui-intab-tabs li").on("click", function () {
                var oldtab = $(".yui-intab-tabs li.current");
                oldtab.removeClass("current");
                var $this = $(this);
                me.currentTab=$this;
                $this.addClass("current");
                var oldtarget = oldtab.attr("data-target");
                $(oldtarget).hide();
                var target = $this.attr("data-target");
                $(target).show();
                el.find(".tabbtns [data-target]").hide();
                el.find(".tabbtns [data-target]").each(function () {
                    var $btn = $(this);
                    if ($this.attr("id") && $btn.attr("data-target") == "#" + $this.attr("id")) {
                        $btn.show();
                    }
                });
                var _iframe = $(target).find("iframe");
                if (_iframe.length > 0 && $this.attr("data-refresh") == "true") {
                    var url = _iframe.attr("data-lazy") || _iframe.attr("src");
                    _iframe.attr("src", "");
                    _iframe.attr("src", url);
                }
                if (_iframe.length > 0 && $this.attr("data-lazy")) {
                    _iframe.attr("src", _iframe.attr("data-lazy"));
                    $this.removeAttr("data-lazy");
                }
            });
            var currentel = el.find(".yui-intab-tabs li.current");
            if (currentel.length > 0)currentel.trigger("click");
        },
        /**
         * 切换选项卡
         * @param finder 如果finderd是整数时，按序号查找
         * @param type
         */
        switchTo: function (finder, type) {
            var me=this;
            if (type) {
                $(me.settings.intabEl).find("[" + type + "='" + finder + "']").trigger("click");
            } else {
                type = Ladder.getType(finder);
                switch (type) {
                    case "Number":
                        $(me.settings.intabEl).find("li:eq(" + finder + ")").trigger("click");
                        break;
                    case "String":
                        var $li = $(me.settings.intabEl).find("li:contains('" + finder + "')");
                        $(me.settings.intabEl).find("li:contains('" + finder + "')").trigger("click");
                        break;
                }
            }
        },
        swapview:function(swapEl){
            var childrens=$(swapEl).children();
            for (var i = 0; i < childrens.length; i++) {
                var $showel = $(childrens[i]).hide();
            }
        },
        getTabCount:function(){
           return this.settings.tabsEL.find("li").length;
        }
    };
    //自动完成组件
    Ladder.NS("AutoComplete");
    Ladder.AutoComplete = function (el, config) {
        this.selectEl= null;
        this.complateEl= null;
        this.complateData=null;
        this.holdvalue="";
        this.settings= {
            startLength: 0,
            queryUrl: null,
            queryData: null,
            height: "100px",
            form:null,
            size:5,
            data:null,
            reader:{data:"data"},//阅读器
            onSelected:null,
            onRequest:null,
            repeatRequest: true,
        };
        config=Ladder.toJSON(config);//假如是json数据
        if(Ladder.getType(config)=="Array"){
            this.settings.data=config;
        }else{
            $.extend(this.settings,config);
        }
        this.init(el);
    };
    Ladder.AutoComplete.prototype = {
        init: function (el) {
            if (!el) return;
            var me = this;
            me.complateEl = $(el);
            if(me.complateEl.length<1)return;
            if(me.complateEl.attr("yui")=="false")return;
            me.complateEl.attr("yui",false);
            me.complateEl.attr("autocomplete","off");
            me.selectEl = $("<select style='display: none'></select>").addClass("yui-complete").appendTo(document.body);
            me.selectEl.attr("size", 5);
            if(me.settings.data)me.complateData=me.settings.data;
            if (me.settings.startLength == 0) {
                me.complateEl.click(function () {
                    var $this = $(this);
                    //me.selectEl.offset(($this).offset());
                    var outline = $this.css("outline-width").replace(/px/i, "") * 1;
                    //各浏览器有差异
                    me.selectEl.css({
                        left: Math.floor($this.offset().left * 1-1),
                        top: $this.offset().top + $this.outerHeight(),
                        width: $this.outerWidth(),
                        "z-index":Ladder.getMaxZ(me.complateEl)+100
                    });
                    me.resetOption();
                });
            }
            me.complateEl.focus(function(){
                me.holdvalue=me.complateEl.val();
            });
            me.complateEl.keyup(function (e) {
                var value=me.complateEl.val().trim();
                if (e.keyCode == 40) {//向下键
                    me.holdvalue=value;
                    me.selectEl.focus();

                    setTimeout(function(){
                        me.selectEl.find("option:first").prop("selected",true).focus();
                    }, 50);
                    return;
                }
                if(e.keyCode == 27){//向上
                    me.selectEl.hide();
                    return;
                }
                if (value.length < me.settings.startLength) return;
                setTimeout(function(){
                    var cv=me.complateEl.val().trim();
                    //上次的输入值和现值相等以及保持值和现值不等
                    if (cv && value == cv && me.holdvalue != cv && (!me.complateData || me.settings.repeatRequest)) {
                        me.holdvalue=cv;
                        if(me.settings.form){
                            var form = Ladder.data({}).set(me.settings.form, cv);
                            if (me.settings.queryData && me.settings.queryData.condition) {
                                form = $.extend(me.settings.queryData.condition, form);
                            }
                            $.extend(me.settings.queryData, { condition: form });
                        }
                        me.requestData();
                    }
                },600);
                me.selectEl.css({
                    left: Math.floor(me.complateEl.offset().left-2),
                    top: me.complateEl.offset().top + me.complateEl.outerHeight(),
                    width: me.complateEl.outerWidth(),
                    "z-index":Ladder.getMaxZ(me.complateEl)+100
                });
                me.resetOption(value);
            });
            me.selectEl.click(function(){
                var $this=$(this);
                me.complateEl.val($this.val());
                me.complateEl.data("AutocompleteData",$this.data("data"));
                if(me.settings.onSelected)me.settings.onSelected($s.data("data")||$this.val());
            });
            me.selectEl.keyup(function (e) {
                var $this = $(this);
                //回车键
                if (e.keyCode == 13) {
                    var $s=$this.find("option:selected,option:first");
                    me.complateEl.val($s.last().val());
                    me.complateEl.data("AutocompleteData",$s.data("data"));
                    me.complateEl.focus();
                    me.selectEl.hide();
                    if(me.settings.onSelected)me.settings.onSelected($s.data("data")||$s.val());
                    return;
                }
                //上键
                if (e.keyCode == 38) {
                    if ($this.find("option:visible").index($this.find("option:selected")) == 0) {
                        me.complateEl.val(me.holdvalue);
                        me.complateEl.focus();
                        var r = me.complateEl[0].createTextRange();
                        r.moveStart("character", me.complateEl[0].value.length);
                        r.collapse(true);
                        r.select();
                        return;
                    }
                }
                //返回键
                if(e.keyCode == 27){
                    me.selectEl.hide();
                    me.complateEl.focus();
                }
                var $s=$this.find("option:selected");
                me.complateEl.val($s.val());
                me.complateEl.data("AutocompleteData",$s.data("data"));
                if(me.settings.onSelected)me.settings.onSelected($s.data("data")||$s.val());
            });
            if (me.settings.queryUrl&&me.settings.startLength==0&&!me.settings.data) {
                me.requestData();
            }
            $(document).on("click.Autocomplete",function(event){
                if (event.target != me.complateEl.get(0)) {
                    me.selectEl.hide();
                }
            });


        },
        requestData:function(){
            var me=this;
            if(me.settings.data){
                return;
            }
            if(me.settings.onRequest){
                var data=me.settings.onRequest(me.settings.queryData);
                if(data){
                    me.createOption(data);
                    me.complateData=data;
                    if(me.selectEl.find("option[data-show=1]").length>0)
                        me.selectEl.show();
                }
                return;
            }
            webajax(me.settings.queryUrl, me.settings.queryData, function (result) {
                me.selectEl.empty();
                if (Ladder.getType(result) == "String") result = Ladder.toJSON(result);
                if (result.hasOwnProperty("statusCode")&&result.hasOwnProperty("data")) { //这是我们自己的后台模版
                    if (result.statusCode == 200) {
                        me.createOption(result.data);
                        me.complateData=result.data;
                    }
                } else {
                    me.createOption(result);
                    me.complateData=result;
                }
                //if(me.selectEl.find("option[data-show=1]").length>0) me.selectEl.show();
            });
        },
        /**
         * 重置选项
         * @param value 过滤值
         * @param data 原型数据
         */
        resetOption:function(value,data){
            var me=this;
            data=Ladder.toJSON(data);
            if(!data)data=me.complateData||me.settings.data;
            if(value==undefined||value==null)value="";
            //使用数据进行过滤
            if(Ladder.getType(data)=="Array"){
                var filterData=data.filter(function(item){
                    if(!value)return true;
                    var t=Ladder.getType(item);
                    if(t=="String"||t=="Number")return (item+"").indexOf(value)>-1;
                    if(t=="Object"){
                        for(var p in item) return (item[p]+"").indexOf(value)>-1;
                    }
                });
                me.selectEl.empty();
                me.createOption(filterData);
            }else{
                //此处写法ie8不过,ie不支持option dispaly,假如您的数据是对象型的数据或者以option过滤,兼容处理
                var options = me.selectEl.find("option");
                if(options.length<1)return;
                for (var i = 0; i < options.length; i++) {
                    var $opt = $(options[i]);
                    if (!(value+"")||$opt.val().indexOf(value+"")>-1) {
                        $opt.attr("data-show",1).show();
                    } else {
                        $opt.attr("data-show",0).hide();
                    }
                }
            }
            if(me.selectEl.find("option[data-show=1]").length>0)me.selectEl.show();
        },
        /**
         * 创建选项
         * @param data 数据
         * @param ext 是否扩展数据存储
         */
        createOption: function (data,ext) {
            var me = this;
            if(data==null||data==undefined)return;
            var type = Ladder.getType(data);
            switch (type) {
                case "Object":
                    for (var propname in data) {
                        me.createOption(data[propname],data);
                        break;
                    }
                    break;
                case "Array":
                    for (var i = 0; i < data.length; i++) {
                        me.createOption(data[i]);
                    }
                    break;

                default:
                    if(me.selectEl.find("option[value='"+data+"']").length<1){
                        var $key = $("<option value='" + data + "' data-show='1'>" + data + "</option>");
                        if(ext)$key.data("data",ext);
                        me.selectEl.append($key);
                        $key.click(function () {
                            me.complateEl.val(data);
                            me.complateEl.data("AutocompleteData",$(this).data("data"));
                            if(me.settings.onSelected)me.settings.onSelected($s.data("data")||$s.val());
                        });
                    }
                    break;
            }
        },
        cancel:function(){
            var me=this;
            me.selectEl.empty().remove();
            me.complateEl.off("keyup");
        }
    };
    //通知组件
    Ladder.NS("Note");
    Ladder.Note=function(el,option){
        this.parentEl=$(el);
        this.contentEl=null;
        if(this.parentEl.length<1)return;
        this.settings={
            radius_head:true,
            radius_foot:true,
            width:"400px",
            height:"200px",
            p_top:"10px",
            small:false,
            content:"",
        };
        $.extend(this.settings,option);
        this.init();
    };
    Ladder.Note.prototype={
        init:function(){
            var $note=$('<div class="yui-note"></div>');
            $note.width(this.settings.width);
            $note.css("padding-top",this.settings.p_top);
            this.parentEl.append($note);
            if(this.settings.radius_head)
                $note.append('<div class="radiu_tl"></div><div class="radiu_tr"></div>');
            if(this.settings.radius_foot)
                $note.append('<div class="radiu_bl"></div><div class="radiu_br"></div>');
            this.contentEl=$('<div class="content"></div>');
            this.contentEl.append(this.settings.content)
            this.contentEl.height(this.settings.height);
            if(this.settings.small) this.contentEl.addClass("small");
            $note.append(this.contentEl);
        },
        content:function(val) {
            if(val===undefined){
                return this.settings.content;
            }
            else{
                this.settings.content=val;
                this.contentEl.empty().html(this.settings.content);
            }
            return this.settings.content;
        }
    };
    Ladder.NS("Tips");
    Ladder.Tips = function (option) {
        this.parentEl = null;
        this.contentEl = null;
        this.html = "";
        this.settings = {
            title: "提示信息",
            width: "300px",
            height: "60px",
            p_top: "10px",
            align:"center",
            button:"我知道了"
        };
        $.extend(this.settings, option);
        this.init();
    };
    Ladder.Tips.prototype= {
        init: function () {
            var $div = $("<div style='width:100%;z-index:9999;position:absolute;top:0;left:0;'></div>");
            var $tips = $("<div class='yui-tips'></div>");
            if (this.settings.align == "center") {
                $tips.css("margin", "0 auto");
            } else if (this.settings.align == "right") {
                $tips.css("float", "right");
            }
            $tips.width(this.settings.width);
            $tips.css("padding-top", this.settings.p_top);
            var $title = $("<div class='tips-title'></div>");
            $title.append("<img src='"+URLS.URL_BasicURL+"/images/tip_icon.png' style='margin:0 5px'  />");
            $title.append(this.settings.title);
            this.contentEl = $('<div class="tips-content">' + this.settings.content + '</div>');
            $tips.append($title);
            $tips.append(this.contentEl);
            this.contentEl.height(this.settings.height);
            var $button = $("<button class='tips-button'></button>");
            var $div_foot = $("<div class='tips-foot'><div>").css("width",this.settings.width);
            $button.html(this.settings.button);
            $div_foot.html($button);
            $tips.append($div_foot);
            $div.append($tips);
            $("body", window.document).append($div);
            $button.on("click", function() {
                $(this).parent().parent().hide();

            });
        }
    };
    Ladder.NS("Picture");
    Ladder.Picture= function(el,url,option){
        this.parentEl=$(el);
        this.url=url;
        if(this.url.length<1)return;
        if(this.parentEl.length<1)return;
        this.settings={
            area:null,//图片框面积,eg:['400','200']
            stretch:false,//是否允许图片拉伸，默认不允许 CSS3.0支持
            title:false,//是否显示标题，默认不显示
            border:false,//边框样式，默认不存在
            position:"center"//在图片框中的显示位置，默认为居中,可选项:"top","bottom"
        };
        $.extend(this.settings,option);
        if($(el).height()>0&&$(el).width()>0){
            this.settings.area=[$(el).height(),$(el).width()]
        }
        this.init();
    };
    Ladder.Picture.prototype={
        init:function(){
            var $imgwh=[];
            var p_top=0;
            var $div=$("<div></div>");
            var me=this;
            Ladder.getImgWh(this.url,function(w,h){
                $imgwh.push(w,h);
                if(me.settings.title!=false){
                    var $title=$("<div>"+me.settings.title+"</div>");
                    me.parentEl.append($title);
                }
                if(me.settings.border!=false){
                    $div.css("border",me.settings.border);
                }
                if(me.settings.area==null){
                    me.settings.area=$imgwh;
                    $div.css({
                        "background":"url("+me.url+")",
                        width:me.settings.area[0],
                        height:me.settings.area[1]
                    });
                    me.parentEl.append($div);
                    return;
                }
                var $img=$("<img src='"+me.url+"'>");
                if(me.settings.area[0]<w){$img.css("width",me.settings.area[0]);$imgwh[1]=me.settings.area[0];}
                if(me.settings.area[1]<h){$img.css("height",me.settings.area[1]);$imgwh[1]=me.settings.area[1];}
                switch (me.settings.position){
                    case "center":p_top=(me.settings.area[1]-$imgwh[1])/2;$div.css("padding-top",p_top);break;
                    case "top":p_top=0;$div.css("padding-top",p_top);break;
                    case "bottom":p_top=me.settings.area[1]-$img.css("height");$div.css("padding-top",p_top);break;
                }
                $div.css({
                    width:me.settings.area[0],
                    height:(me.settings.area[1]*1-p_top),
                    "text-align":"center"
                });
                $div.append($img);
                me.parentEl.append($div);
            });
        }
    };
    Ladder.NS("TableCombox");
    /**
     * 表格选择器组件
     * @param el 触发元素
     * @param option  配置项包括：表格宽度，查询条件，分页条件等等。
     * @constructor
     */
    Ladder.TableCombox= function(el,config){
        this.parentEl=$(el);
        if(this.parentEl.length<1)return;
        if(this.parentEl.attr("yui")=="false")return;
        this.parentEl.attr("yui",false);
        this.table=$("<div></div>");
        this.load=0;//加载类型
        this.condition=null;
        this.settings={
            id:Ladder.uuid(20),
            queryUrl: null,
            queryData: {},
            tableName:null,//YiFeng库专用属性，有此属性自动赋值一些配置值，外部使用可忽略
            mode:"table",
            width:"300",
            height:"auto",
            data:"",//原始数据
            title:"请选择相应的数据行",
            reader:{
                records:"data.records",//记录数据
                columns:"data.columns",//列模型
                "queryform":"data.queryform",//表单数据
                pager:"data.condition.pager",//分页数据
                condition:"data.condition",//条件数据
            },
            queryform:null,
            multiSelect:"only",
            chooser:null,
            columns:null,//格式:[{fieldname:"name",title:"姓名"}]
            columnConfig:null,
            onRequest:null,
            size:10,
            readonly:true,
            repeatRequest:false,
        };
        config=Ladder.toJSON(config);
        var type = Ladder.getType(config);
        switch (type){
            case "Array":
                this.settings.data=config;
                break;
            case "Object":
                $.extend(this.settings,config);
                break;
            case "String":
                this.settings.queryUrl=config;
                break;
            default:
                return;
        }
        //YiFeng库专用属性
        if(this.settings.datasource){
            var json=Ladder.toJSON(this.settings.datasource);
            this.settings.queryUrl = URLS.QueryData;
            switch (Ladder.getType(json)){
                case "String":
                    this.settings.queryData = { tableName: json,condition:{},psize:this.settings.size};
                case "Object":
                    this.settings.queryData=json;
                    if(json.chooser)this.settings.chooser=json.chooser;
                    if(json.columns){
                        if(json.condition)json.condition.columnString=json.columns;
                        else json.condition={columnString:json.columns};
                    }
            }
        }
        //YiFeng库专用属性
        if (this.settings.tableName) {
            this.settings.queryUrl = URLS.QueryData;
            this.settings.queryData = { tableName: this.settings.tableName,condition:{},psize:this.settings.size };
            if(this.settings.condition)this.settings.queryData.condition=this.settings.condition;
            if(Ladder.getType(this.settings.columns)=="String"){
                this.settings.queryData.condition["columns"]=this.settings.columns;
                delete this.settings.columns;
            }
        }
        this.settings.chooser=Ladder.toJSON(this.settings.chooser);
        this.init();
    };
    Ladder.TableCombox.prototype={

        init:function(){
            var me = this;
            me.table.attr("id",me.settings.id).appendTo(document.body).hide();
            me.parentEl.prop("readonly",me.settings.readonly).on("click", function (event) {
                // Ladder.stopPropagation(event);
                var $this=$(this);
                $this.val($this.val().replace(/,$/,""));
                me.__resetSelected();
                $(document).off("click."+me.settings.id);
                $(document).on("click."+me.settings.id,function(event){
                    if ($(event.target).parent().length<1||$(event.target).parents("#"+me.table.attr("id")).length>0||$(event.target).is(me.parentEl)) return;
                    me.table.hide();
                    $(document).off("click."+me.settings.id);
                });
                if(!me.table.is(":hidden"))return;
                me.table.css({
                    left: me.parentEl.offset().left,
                    top: me.parentEl.offset().top + me.parentEl.outerHeight() + 3,
                    "z-index":Ladder.getMaxZ(me.parentEl)+100
                });
                me.table.show();
                if(me.table.find(".select-table-head").length>0 && !me.settings.repeatRequest){
                    return;
                }
                if(me.load!="local"){
                    me.load=0;
                    me.table.empty();
                    me.requestData();
                }
            });
            if (me.settings.data) {
                me.load = "local";
                var records = null;
                if (Ladder.getType(me.settings.data) == "String") {
                    me.settings.data = Ladder.toJSON(me.settings.data);
                }
                if (Ladder.getType(me.settings.data) == "Object") {
                    var data = Ladder.data(me.settings.data);
                    records = data.get("*.records");
                    if (!me.settings.columns)me.settings.columns = data.get("*.columns");
                    if (!me.settings.queryform)me.settings.queryform = data.get("*.queryform");
                    me.settings.data = records;
                }
                if (Ladder.getType(me.settings.data) == "Array") {
                    //以文本数组形式的初值
                    if (Ladder.getType(me.settings.data[0]) === "String" && !/^\s*\{/.test(me.settings.data[0])) {
                        var records = [];
                        if (!me.settings.columns)
                            me.settings.columns = [{ title: me.settings.title != "请选择相应的数据行" ? me.settings.title : "数据字典", fieldname: "textdata" }];
                        me.settings.data.forEach(function(item) {
                            records.push({ textdata: item });
                        });
                        me.settings.data = records;
                    } else {
                        records = me.settings.data;
                    }
                }
                me.condition = {
                    pager: { pageNumber: 1, pageCount: (me.settings.size ? Math.ceil(me.settings.data.length / me.settings.size) : 0) }
                };
                me.filldata({
                    data: {
                        records: records.slice(0, (me.settings.size > 0 ? me.settings.size : records.length)),
                        condition: me.condition
                    }
                }, false);
            }
//            else me.requestData();
        },
        /**
         * 向表格填充数据
         * @param result
         */
        filldata:function(data,isshow){
            if(!data)return;
            var result= Ladder.data(data);
            var me=this;
            if(!result.get(me.settings.reader.records)){
                me.table.css("display","none");
                return;
            }
            else {
                if (isshow || isshow == undefined)me.table.css("display", "block");
            }
            me.table.css({
                width:me.settings.width<210?210:me.settings.width,
                height:"auto",
                position: "absolute",
                left: me.parentEl.offset().left,
                top:  me.parentEl.offset().top+me.parentEl.outerHeight(),
                "z-index":Ladder.getMaxZ(me.parentEl)+100,
                "background-color":"#ffffff",
                "border-collapse": "collapse",
                "border":"1px solid #ccc"
            });
            me.table.empty();
            var queryArea = $("<div class='select-query-area background-b color-a'></div>").appendTo(me.table);
            var $table = $("<table class='select-table'></table>").appendTo(me.table);
            $table.append("<thead class='select-table-head background-b color-a'><tr></tr></thead>");
            $table.append("<tbody class='select-table-body color-a'></tbody>");
            $("<div class='table-fill-blank  background-b color-a'></div>").appendTo(me.table).height(0);
            $("<div class='select-table-foot  background-b color-a'></div>").appendTo(me.table).hide();


            //关闭按钮
            $("<a class='close'></a>").appendTo(queryArea).click(function(){
                me.table.hide();
            });
            //queryform区域
            var queryforms=me.settings.queryform||result.get(me.settings.reader.queryform+".list");
            if (queryforms) {//填充查询区域
                for (var i = 0; i < queryforms.length; i++) {
                    queryArea.append("<label>" + (queryforms[i].title||queryforms[i].label) + "</label><input data-name='"+queryforms[i].formname+"' >");
                }
                var inputs=queryArea.find("input");
                var keyuuid=0;
                for(var i=0;i<inputs.length;i++){
                    $(inputs[i]).on("input propertychange",function(){

                    //$(inputs[i]).on("blur",function(){
                        var $this=$(this);
                        var tt=$this.val();
                        setTimeout(function () {
                            var value=$this.val();
                            if(tt!=value)return;
                            if(!me.condition.forms)me.condition.forms={};//获取查询条件，重新组装表格查询
                            me.condition.pager=null;
                            //var value=$(this).val();
                            me.condition.forms[$this.attr("data-name")]=value;
                            if(me.load!="local")me.load=1;
                            me.requestData();
                        },500);

                    });
                }
            }
            else{
                queryArea.find("h1.caption").remove();
                queryArea.append("<h1 class='caption'>" + (me.settings.title ? me.settings.title : "请选择相应的数据行") + "</h1>");
            }
            var cms= me.settings.columns||result.get(me.settings.reader.columns);
            if(!cms){
                cms=[];
                for(var f in result.get(me.settings.reader.records)[0]) cms.push({title:f,fieldname:f });
                me.settings.columns=cms;
            }
            var $tableHead = me.table.find(".select-table-head>tr").append("<th class='choose' style='width: 30px'>选择</th>");
            if(me.settings.multiSelect==true){
                $tableHead.find(".choose").html("全选");
                $tableHead.find(".choose").on("click", function () {
                    var $this = $(this);
                    var $selects=me.table.find("tbody tr td:nth-child(1) input.js-select");
                    if($selects.prop("checked")){
                        $selects.prop("checked", false);
                    }
                    else{
                        $selects.prop("checked", true);
                    }
                    var $tr=me.table.find("tbody tr");
                    $tr.each(function () {
                        $(this).click();
                    });
                });
            }

            cms.forEach(function(cm,i){
                if(me.settings.columnConfig&&me.settings.columnConfig.hasOwnProperty([cm.fieldname])){
                    var config = me.settings.columnConfig[cm.fieldname];
                    switch (Ladder.getType(config)){
                        case "String":
                            cm.title=config;
                            break;
                        case "Number":
                            if(config==0)cm.isshow=0;
                            else cm.isshow=1;
                            break;
                        case "Boolean":
                            cm.isshow=config;
                            break;
                        case "Object":
                            $.extend(cm,config);
                            break;
                    }
                }
                var $th=$("<th data-index='" + i + "' data-fieldname='" + cm.fieldname + "'>" + cm.title + "</th>").appendTo($tableHead);
                if(cm.isshow===0||cm.isshow===false)$th.hide();
            });
            if(!me.settings.columns)me.settings.columns=cms;
            me.updateData(result);
        },
        //重新加载数据
        updateData:function(result){
            var me=this;
            var $tbody=me.table.find(".select-table-body").empty();
            var records=result.get(me.settings.reader.records);
            var $blank=me.table.find(".table-fill-blank").height(0).empty();
            if(!records){
                if(me.settings.height=="auto" || me.table.height()>me.settings.height){
                    $blank.height(80).append("<div style='width: 100%;height: 100%;padding-top: 35px;text-align: center;font-weight: bold'>没有符合条件的数据</div>");
                }else{
                   var diffheight = me.settings.height- me.table.height();
                    $blank.height(diffheight).append("<div style='width: 100%;padding-top: "+((diffheight/2)-20)+"px;height: 100%;font-weight: bold;text-align: center'>没有符合条件的数据</div>");
                }
                me.table.find(".select-table-foot").hide();
                return;
            }
            me.condition=result.get(me.settings.reader.condition);
            var cms= me.settings.columns||result.get(me.settings.reader.columns);
            records.forEach(function(record){
                var $tr=$("<tr></tr>").appendTo($tbody).data("data",record);
                $tr.on("click",function(event){
                    Ladder.stopPropagation(event);
                    var multi =(me.settings.multiSelect=="only" || !me.settings.multiSelect)?false:true;
                    if(!multi)$(this).find(".js-select").prop("checked",true);
                    var checked=$(this).find(".js-select").prop("checked");
                    if(!me.settings.chooser){
                        if(!multi)  me.parentEl.val($(this).find("td:eq(1)").html());
                        else{
                            var old=me.parentEl.val();
                            old=old?old:"";
                            var cv=$(this).find("td:eq(1)").html();
                            if(!checked)me.parentEl.val((","+old+",").replace(","+cv+",",",").replace(/,$/,"").replace(/^,/,""));
                            else if((","+old+",").indexOf(","+cv+",")<0){
                                me.parentEl.val(old+(old?",":"")+cv);
                            }
                        }
                    }
                    else{
                        var rowData=Ladder.data($(this).data("data"));
                        me.settings.chooser=Ladder.toJSON(me.settings.chooser);
                        var type=Ladder.getType(me.settings.chooser);
                        switch (type){
                            case "String":
                                if(!multi)me.parentEl.val(rowData.get(me.settings.chooser));
                                else{
                                    var old=me.parentEl.val();
                                    old=old?old:"";
                                    var cv=rowData.get(me.settings.chooser);
                                    if(!checked)me.parentEl.val((","+old+",").replace(","+cv+",",",").replace(/,$/,"").replace(/^,/,""));
                                    else if((","+old+",").indexOf(","+cv+",")<0){
                                        me.parentEl.val(old+(old?",":"")+cv);
                                    }
                                }
                                me.parentEl.attr("data-cfn",me.settings.chooser);
                                break;
                            case "Object":
                                for(var pname in me.settings.chooser){
                                    var key=me.settings.chooser[pname];
                                    var v=rowData.get(key);
                                    if(pname.toLowerCase()=="value"){
                                        if(!multi)me.parentEl.val(v).attr("value",v);
                                        else{
                                            var old=me.parentEl.val()||me.parentEl.attr("value");
                                            old=old?old:"";
                                            if(!checked) me.parentEl.val((","+old+",").replace(","+v+",",",").replace(/,$/,"").replace(/^,/,""));
                                            else if((","+old+",").indexOf(","+v+",")<0){
                                                me.parentEl.val(old+(old?",":"")+v);
                                            }
                                        }
                                        me.parentEl.attr("data-cfn",key);
                                    }else{
                                        if(!multi){
                                            me.parentEl.attr(pname,v);
                                        }else{
                                            var old=me.parentEl.attr(pname);
                                            old=old?old:"";
                                            if(!checked)me.parentEl.attr(pname,(","+old+",").replace(","+v+",",",").replace(/,$/,"").replace(/^,/,""));
                                            else if((","+old+",").indexOf(","+v+",")<0){
                                                me.parentEl.attr(pname,old+(old?",":"")+v);
                                            }
                                        }
                                    }
                                }
                                break;
                            case "Number":
                                if(!multi)me.parentEl.val($(this).find("td:eq("+me.settings.chooser+")").html());
                                else{
                                    var old=me.parentEl.val();
                                    var cv=$(this).find("td:eq("+me.settings.chooser+")").html();
                                    if(!checked)me.parentEl.val((","+old+",").replace(","+cv+",",",").replace(/,$/,"").replace(/^,/,""));
                                    else if((","+old+",").indexOf(","+cv+",")<0){
                                        me.parentEl.val(old+(old?",":"")+cv);
                                    }
                                }
                                me.parentEl.attr("data-cfn",me.table.find(".select-table select-table-head th:eq("+me.settings.chooser+")").attr("data-fieldname"));
                                break;
                            case "Function":
                                me.settings.chooser(rowData);
                                break;
                        }
                    }
                });
                var tableid=me.table.attr("id");
                if(me.settings.multiSelect=="only" || !me.settings.multiSelect){
                    $tr.append("<td><input type='radio' name='radio"+tableid+"' class='js-select'></td>");
                }else $tr.append("<td><input type='checkbox' name='checkbox"+tableid+"' class='js-select'></td>");
                cms.forEach(function(cm){
                    var $td=$("<td align='center' data-fieldname='"+cm.fieldname+"'></td>").appendTo($tr);
                    $td.html(record[cm.fieldname]?record[cm.fieldname]:"");
                    if(cm.isshow===0||cm.isshow===false)$td.hide();
                });
                if( me.parentEl.val()){
                    var pname  = me.parentEl.attr("data-cfn");
                    if(!pname){
                        switch (Ladder.getType(me.settings.chooser)){
                            case "Null":
                            case "Undefined":
                                pname=cms[0].fieldname;
                                break;
                            case "String":
                                pname=me.settings.chooser;
                                break;
                            case "Object":
                                pname=me.settings.chooser.value;
                                break;
                            case "Number":
                                pname=cms[me.settings.chooser].fieldname;
                                break;
                        }
                    }
                    if(!pname)return;
                   var choosedstring= ","+me.parentEl.val()+",";
                   if(record[pname]!=undefined&&choosedstring.indexOf(","+record[pname]+",")>-1){
                       $tr.find(".js-select").prop("checked",true)
                   }
                }
            });
            var $foot=me.table.find(".select-table-foot").hide();
            var pager=result.get(me.settings.reader.pager);
            if (pager&&pager.pageCount>0) {//处理分页
                $foot.empty();
                $foot.show();
                $("<button class='nextpage'>下一页 ></button>").appendTo($foot).click(function(event){
                    Ladder.stopPropagation(event);
                    var number=pager.pageNumber;
                    if(number==pager.pageCount)return;
                    else{
                        result.set(me.settings.reader.pager+".pageNumber",number+1);
                        if(me.load!="local")me.load=1;
                        me.requestData();
                    }
                });
                $("<button class='backpage'>< 上一页</button>").appendTo($foot).click(function(event){
                    Ladder.stopPropagation(event);
                    var number=pager.pageNumber;
                    if(number==1)return;
                    else{
                        result.set(me.settings.reader.pager+".pageNumber",number-1)
                        if(me.load!="local")me.load=1;
                        me.requestData();
                    }
                });
                $("<span style='padding: 4px 0 0 5px;display: inline-block'>页面: "+pager.pageNumber+" / "+pager.pageCount+"</span>").appendTo($foot);
            }

           if(me.settings.height>20&&me.settings.height - me.table.height()>0){
               $blank.height(me.settings.height - me.table.height());
           }
        },
        requestData:function(){//向后台请求数据
            var me=this;

            if(me.settings.queryData.hasOwnProperty("condition")&&me.condition){
                var _condition= $.extend(me.settings.queryData["condition"],me.condition);
                me.settings.queryData["condition"]=_condition;
            }
            if(me.settings.data){
                if(!me.condition)me.condition={};
                var records=me.settings.data.slice(0);
                if(me.condition.forms){
                    for(var name in me.condition.forms){
                        var v=me.condition.forms[name];
                        if(v.length<1)continue;
                        var cname=name.split(":")[0];
                        records=records.filter(function(item){
                            return (item.hasOwnProperty(cname)&&item[cname]!=undefined&&(item[cname]+"").indexOf(v)>-1);
                        });
                    }
                }
                if(!me.condition.pager){
                    me.condition.pager={
                        pageNumber:1,
                        pageCount:Math.ceil(records.length/me.settings.size),
                    }
                }
                //var end=me.condition.pager.pageNumber*me.settings.size>recorme.settings.data.length?recorme.settings.data.length:me.condition.pager.pageNumber*me.settings.size;
                me.updateData(Ladder.data({
                    data:{
                        records:records.slice((me.condition.pager.pageNumber-1)*me.settings.size,me.condition.pager.pageNumber*me.settings.size),
                        condition:me.condition
                    }
                }));
                return;
            }
            if(me.settings.onRequest){
                me.settings.onRequest(me.condition);
                return;
            }
            if (!me.settings.queryUrl)return;
            webajax(me.settings.queryUrl,me.settings.queryData,function(result){
                if(me.load==0)me.filldata(result);
                if(me.load==1)me.updateData(Ladder.data(result));
                return;
            });
        },
        addQueryForm:function(formname,label){
            if(!fieldname)return;
            if(!this.settings.queryform)this.settings.queryform=[];
            if(!label)label=fieldname;
            this.queryform.push({label:title,formname:formname});
        },
        /***
         * 设置数据
         * @param data 数据
         * @param isappend 是否追加
         */
        setData:function(data) {
            var me=this;
            var records=me.settings.data;
            if(Ladder.getType(data)=="Array") {
                records=data;
            }
            me.condition={
                pager:{pageNumber:1,pageCount:(me.settings.size?Math.ceil(me.settings.data.length/me.settings.size):0)}
            };
            me.filldata({
                data:{
                    records:records.slice(0,(me.settings.size>0?me.settings.size:records.length)),
                    condition:me.condition
                }
            },false);
        },
        __resetSelected:function() {
            var me = this;
            me.table.find("input[type=radio],input[type=checkbox]").prop("checked",false);
            if((""+me.parentEl.val()).length<1)return;
            var pName = me.parentEl.attr("data-cfn");
            if (!pName)return;
            var cStr = "," + me.parentEl.val() + ",";
            me.table.find(".select-table-body tr").each(function () {
                var $tr=$(this);
                var record =$tr.data("data");
                if (record[pName] != undefined && cStr.indexOf("," + record[pName] + ",") > -1) {
                    $tr.find(".js-select").prop("checked", true)
                }
            });
        }
    };

    /***
     * 复合选择器
     */
    Ladder.NS("CompoundSelect");
    /***
     * 复合选择器构造
     * @param el {String/dom/$} 元素选择器
     * @param option {Object} 配置选项
     * @param data {Array,Object,String} 初始化数据
     * @constructor
     */
    Ladder.CompoundSelect=function(el,option,data){
        this.SelectData=data;//选项的数据
        this.parentEl= null;//父级元素
        this.selectEl= null;//下拉元素
        this.inputEl=null;//搜素输入框
        this.compoundselectEL=null;
        this.settings= {
            offsetTop:0,
            /**@cfg {int} 输入起始联想字数*/
            startLength: 0,
            /**@cfg {String} 请求地址*/
            queryUrl: "",
            /**@cfg {String} 请求参数*/
            queryData: "",
            /**@cfg {String} 选框高度*/
            height: "200px",
            /**@cfg {int} 备选数量*/
            size:5,
            width:"200px",
            /**@cfg {String} 值数据对应的键*/
            value:"value",
            /**@cfg {String} 标题数据对应的键*/
            title:"title",
            fetchdata:[],//获取数据属性选项
            /**@cfg {function} 选择后回调事件*/
            onSelected:null
        };
        option=$.extend(this.settings,option);
        this.runstaut={requested:false};
        this.parentEl=$(el);
        if(this.parentEl.length<1)return;
        this.init(option);
    };
    Ladder.CompoundSelect.prototype={
        /***
         * 初始化方法
         * @param option 配置选项
         */
        init: function (option) {
            var me = this;
            me.compoundselectEL=$("<div style='position: absolute;display: none;width:"+me.settings.width+"' class='yui-CompoundSelect'></div>").appendTo($(document.body));
            me.inputEl=$("<input style='width: 98%'>").appendTo(this.compoundselectEL).css("background","#fff url('../../images/shareImages/search_btn.png') no-repeat right");
            me.selectEl = $("<select style='width:"+me.settings.width+";height: "+me.settings.height+"' class='MultiSelect'></select>").attr("size", 5).appendTo(this.compoundselectEL);
            me.parentEl.click(function (e) {
                Ladder.stopPropagation(e);
                var $this = $(this);//父级关联元素
                if(!me.compoundselectEL.is(":hidden")){
                    me.compoundselectEL.hide();return;
                }
                me.selectEl.empty();
                if(me.SelectData){
                    for(var i= 0,length=me.SelectData.length;i<length;i++)
                        me.selectEl.append(me.SelectData[i].el);
                }
                me.compoundselectEL.show().css({
                    position:"absolute",
                    left: $this.offset().left-1,
                    top: $this.offset().top + $this.outerHeight()+me.settings.offsetTop
                });
                me.inputEl.focus();
                var arr=me.selectEl.find("option:contains('"+$this.val()+"')");
                if(arr.length>0&&$this.val()!=""){
                    me.selectEl.find("option:contains('"+$this.val()+"')").prop("selected",true);
                }
                $(document).click(function (event) {
                    if (event.target.parentElement != me.selectEl.get(0)) {
                        me.compoundselectEL.hide();
                        $(this).off();
                    }
                });
            });
            me.inputEl.keyup(function (e) {
                if (e.keyCode === 40) {
                    me.selectEl.focus();
                }
                var $this = $(this);
                if ($this.val().length < me.settings.startLength) return;
                if(!me.runstaut.requested && me.settings.queryUrl){
                    me.requestData();
                }
                if(me.SelectData){
                    me.selectEl.empty();
                    for(var i= 0,length=me.SelectData.length;i<length;i++){

                       var reg =new RegExp($this.val(),"i");
                       if(reg.test(me.SelectData[i].title)) {
                           me.selectEl.append(me.SelectData[i].el);
                       }
                        //if(me.SelectData[i].title.indexOf($this.val())>-1){
                        //    me.selectEl.append(me.SelectData[i].el);
                        //}
                    }

                }
            });
            //选框内点击事件
            me.selectEl.dblclick(function(event){
                if(me.settings.onSelected){
                    me.settings.onSelected(me.selectEl.find("option:selected").val(),me.selectEl.find("option:selected").html());
                }else{
                    me.parentEl.attr("data-value",me.selectEl.find("option:selected").val());
                    me.inputEl.html("");
                    me.parentEl.val(me.selectEl.find("option:selected").html());
                    me.parentEl.html(me.selectEl.find("option:selected").html());
                }
                me.compoundselectEL.hide();
                me.parentEl.focus();
                Ladder.stopPropagation(event);//禁止事件冒泡
            });
            //选择框内键盘事件
            me.selectEl.keyup(function (e) {
                var $this = $(this);
                //回车键
                if (e.keyCode == 13) {
                    if(me.settings.onSelected){
                        me.settings.onSelected($this.find("option:selected").val(),me.selectEl.find("option:selected").html());
                    }else{
                        me.parentEl.attr("data-value",me.selectEl.find("option:selected").val());
                        me.parentEl.val(me.selectEl.find("option:selected").html());
                        me.parentEl.html(me.selectEl.find("option:selected").html());
                        me.inputEl.html("");
                    }
                    me.compoundselectEL.hide();
                    me.parentEl.focus();
                }
                //向上键
                if (e.keyCode == 38) {
                    if ($this.find("option:visible").index($this.find("option:selected")) == 0) {
                        me.inputEl.focus();
                    }
                }
            });

            if(me.SelectData)me.addData(me.SelectData,true);
            if (option.queryUrl) me.requestData();
        },
        requestData:function(url,data){
            var me=this;
            if(!url)url=me.settings.queryUrl;
            if(!data)data=me.settings.queryData;
            webajax(url,data, function (result) {
                if (Ladder.getType(result) === "String") result = Ladder.toJSON(result);
                if (result.hasOwnProperty("statusCode")) {
                    if (result.statusCode === 200) {
                        me.runstaut.requested =true;
                        me.selectEl.empty();
                        if(result.data==null) return;
                        me.addData(result.data,true);
                    }
                } else {
                    me.runstaut.requested =true;
                    me.selectEl.empty();
                    me.addData(result,true);
                }
            });
        },
        addData: function (data,isReBulidData) {
            var me = this;
            if(!data)data=me.SelectData;//置换数据
            if(!data)return;//如果数据不存在返回
            var type = Ladder.getType(data);
            switch (type) {
                case "Object":
                    if(data.hasOwnProperty('records')){
                        for(var sd= 0,length=data.columns.length;sd<length;sd++){
                            if(data.columns[sd].fieldname!="rn") {
                                me.settings.fetchdata.push(data.columns[sd].fieldname);
                            }
                        }
                        me.settings.fetchdata.push("jp");
                        me.addData(data.records);

                        break;
                    }
                    var datastring="";
                    for (var propname in data) {
                        datastring=data[propname];
                        break;
                    }
                    var v=data.hasOwnProperty(me.settings.value)?data[me.settings.value]:datastring;
                    var t=data.hasOwnProperty(me.settings.title)?data[me.settings.title]:datastring;
                    addOption(v,t,data);
                    break;
                case "Array":
                    for (var i = 0; i < data.length; i++) {
                        me.addData(data[i]);
                    }
                    break;
                default:
                    addOption(data,data);
                    break;
            }

            if(isReBulidData){
                me.SelectData=[];
                me.selectEl.find("option").each(function(index){
                    var title="";
                    if(me.settings.fetchdata.length>0){
                        for(var s= 0,len=me.settings.fetchdata.length;s<len;s++){
                            title=title+$(this).attr("data-"+me.settings.fetchdata[s])+"";
                        }

                    }
                    me.SelectData.push({title:title,el:$(this)});
                });
            }

            //添加选项
            function addOption(value,title){
                value = value.trim();
                if(me.selectEl.find("option[value='"+value+"']").length<1){

                    var $key = $("<option value='" + value + "'>" + title + "</option>");
                    $key.attr("data-jp",makePy(title));

                    for(var k in data){
                        $key.attr("data-"+k,data[k]);
                    }
                    me.selectEl.append($key);

                }
            }

        }

    }
    Ladder.NS("TabBox");
    Ladder.TabBox=function(name,option){
        this.fel=$("iframe#"+name);
        this.settings={
            width:"430px",
            height:"361px",
        };
        if(this.fel.length<1){
            this.fel = $("<iframe id='"+name+"' style='position: absolute;left:50%;top:50%;z-index: 9999'></iframe>").hide().appendTo(document.body);
        }
        $.extend(this.settings,option);
        this.init(this.settings);
    };
    Ladder.TabBox.prototype={
        init:function(settings){
            this.fel.css({
                width:settings.width,height:settings.height
            });
            if(settings.url){this.fel.attr("src",settings.url)}
        },
        show:function(){
            var me=this;
            var w=$(window).width();
            var h=$(window).height();
            this.fel.show().css({
                left:"50%",
                top:"50%",
                "margin-top":-(parseInt(me.settings.height.replace("px",""))/2),
                "margin-left":-(parseInt(me.settings.width.replace("px",""))/2),
            });

        }
    }

    //片贴
    Ladder.NS("TackTips");
    Ladder.TackTips=function(option,data){
        this.ms=[];
        this.settings={
            top:"50px",
            left:"50px",
            width:"100px",
            height:"20px",
            editable:true
        };

        this.ContextMenu=[
            [{
                text: "切换到编辑模式",
                func: function () {
                    $(this).trigger("click");
                }
            }],
            [{
                text: "切换到显示模式",
                func: function () {

                }
            }],
            [{
                text: "删除",
                func: function () {
                    $(this).parent().remove();
                }
            }]
        ];

        if(data){

        }
    };
    Ladder.TackTips.prototype={
        create:function(data){
            var $el=$("<div class='tacktips'>" +
                    "<div class='content'>"+
                        "<span class='display' style='display: none'></span>" +
                        "<textarea class='editor'></textarea>" +
                    "</div>" +
                    "<div class='arrows'></div>" +
                "</div>").appendTo(document.body);
            $el.css({
                top:data.top?data.top:this.settings.top,
                left:data.top?data.left:this.settings.left
            });
            if(data.editable===undefined||data.editable===true){
                $el.find(".editor").css({
                    width:data.width?data.width:this.settings.width,
                    height:data.height?data.height:this.settings.height,
                }).val(data.content);
            }else{
                $el.find(".display").css({
                    width:data.width?data.width:this.settings.width,
                    height:data.height?data.height:this.settings.height,
                }).show().html(data.content);
                $el.find(".editor").hide();
            }
            $el.find(".arrows").mousedown(function(){
                var $this=$(this).parent();
                $this.data("isMove",true);
                var abs_x = event.pageX - $this.offset().left;
                var abs_y = event.pageY - $this.offset().top;
                $(document).mousemove(function (event) {
                    if ($this.data("isMove")) {
                        $this.css({'left': event.pageX - abs_x, 'top': event.pageY - abs_y});
                    }
                });
            }).mouseup(function () {
                $(this).parent().data("isMove",true);
                $(document).off("mousemove");
            }).smartMenu(this.ContextMenu,{name:"TackTipsMenu",textLimit:10});
        },
        removeAll:function(){
            $(".tacktips").remove();
        }
    };
    /**
     * 数据块
     */
    Ladder.NS("DataBlock");
    Ladder.DataBlock=function(opt){
        this.settings={
            type:"text",
        };
        if(opt){
            opt=$.extend(this.settings,opt);
            return this.init(opt)
        }
    };
    Ladder.DataBlock.prototype={
        init:function(opt){
            opt=$.extend(this.settings,opt);
            if(!opt.data)return;
            var me=this;
            opt.data=Ladder.toJSON(opt.data);
            var type=Ladder.getType(opt.data);
            var els=[];
            if(type=="Array"){
                opt.data.forEach(function(item){
                    els.push(me.create(item));
                });
            }else{
                els.push(me.create(opt.data));
            }
            return els;
        },
        create:function(data){
            var $block=$('<div class="DataBlock"></div>');
            var $caption=$('<div class="caption"></div>');
            $caption.append("<span>"+this.settings.title+"</span>").appendTo($block);
            $caption.append('<a class="link" href="#">查详</a>');
            var $dataview=$('<div class="dataview"></div>').appendTo($block);
            if(this.settings.type=="text"){
                $dataview.append('<div class="text">'+data+'</div>')
            }else{
                var $table=$('<div class="keyvalue"><table></table></div>').appendTo($dataview);
                $table=$table.find("table");
                for(var d in data){
                    $table.append('<tr><td>'+d+'</td><td>'+data[d]+'</td></tr>')
                }
            }
            return $block;
        }

    }
    Ladder.NS("JsonShow");
    Ladder.JsonShow=function(el,option){
        this.content="";
        this.template="";
        this.settings={
            el:"",
            event:"dblclick",
            title:"编辑Json数据视图",
            width:'800px',
            height:"550px"
        };
        var type = Ladder.getType(el);
        switch (type){
            case "String":
                this.settings.el=el;
                this.template=$(this.settings.el).attr("data-template");
                break;
            case "Object":
                if(el instanceof $){
                    this.settings.el=el;
                }else{
                    $.extend(this.settings,el);
                }
                this.template=$(this.settings.el).attr("data-template");
                break;
        }
        if(option){
            $.extend(this.settings,option);
            this.template=$(this.settings.el).attr("data-template");
        }
        var me=this;
        $(el).off("dblclick.JsonShow").on("dblclick.JsonShow",function(){
            me.show($(this).val());
        });
    };
    Ladder.JsonShow.prototype={
        show:function(content,callback){
            var me=this;
            if(content){
                me.content=content;
            }else{
                me.content=$(me.settings.el).val();
            }
            Ladder.setStore("tempData",Ladder.toJSON(me.content,true));
            layer.open({
                type: 2,
                title: me.settings.title,
                shadeClose: true,
                area: [(me.settings.width+"").replace(/px/i,"")+"px",(""+me.settings.height).replace(/px/i,"")+"px"],
                content:Ladder.BasicURL+"/page/JsonShow.html"+(me.template?"?tpl="+encodeURI(me.template):""),
                fix:false,
                shade:0.9,
                btn:["保存回值","关闭取消"],
                yes:function(index,layero){
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    var revalue=iframeWin.editor.getText();
                    if(callback){
                        var ret = callback(revalue,iframeWin.editor,index);
                        if(ret===false)return;
                        layer.close(index);
                    }
                    else {
                        $(me.settings.el).val(iframeWin.editor.getText());
                        layer.close(index);
                    }
                }
            });
        },
        setTemplate:function(template){
            this.template=template;
        }
    }

    Ladder.NS("Accordion");
    Ladder.Accordion=function(el,option){
        var settings={
            title:"",
            onExpand:null
        };
        $.extend(settings,option);
        el=$(el);
        if(el.length<1)return;
        var widgets=[];
        function collapse(widget,state){
            if(!widget)return;
            $(widget).each(function(){
                var $widget=$(this);
                if(state==null)state=onoff;
                var $toggle=$widget.find(".toggle");
                var $content=$widget.find(".content:first");
                if(!$toggle.hasClass("iconfont"))$toggle.addClass("iconfont");
                if(state){
                    $content.show(400,function(){
                        $toggle.html("&#xe664;");
                        // $widget.removeClass("yui-accordion-collapse");
                        if(settings.onExpand){
                            settings.onExpand($widget,state);
                        }
                    });
                }else{
                    $content.hide(400,function(){
                        $toggle.html("&#xe661;");
                        // $widget.addClass("yui-accordion-collapse");
                    });
                }
            });
        }
        el.each(function(){
            var onoff=true;
            var $this=$(this);
            if($this.hasClass("yui-accordion")){
                widgets.push($this);
                settings.background=$this.css("background-image");
                if($this.find(".head").length<1)$this.append('<div class="head"></div>');
                var $head=$this.find(".head:first").attr("unselectabl", "on").attr("onselectstart", "return false;");
                if($this.find(".title").length<1)$head.append('<div class="title"><span>'+settings.title+'</span></div>');
                $head.click(function(){ collapse($this,$content.is(":hidden"));});
                if($this.find(".toggle").length<1)$head.append('<i class="iconfont toggle">&#xe664;</i>');
                if($this.find(".content").length<1)$this.append('<div class="content"></div>');
                var $content=$this.find(".content:first");
                //if(settings.height)$content.height(settings.height);
                if(settings.width){
                    $this.width(settings.width);
                    $content.width(settings.width);
                }
                if(settings.style)$content.css(style);
                $this.find(".toggle").click(function(){
                    Ladder.stopPropagation(event);
                    collapse($this,$content.is(":hidden"));
                });
                if($content.is(":hidden")){
                    collapse($this,0);
                }
            }
            //以此标签为父标签创建
            else{
                var $content = $('<div class="content"></div>').append($this.children());
                var widget=$("<div class='yui-accordion'></div>").appendTo($this);
                widgets.push(widget);
                var $head=$('<div class="head" onselectstart="return false" unselectabl="on">' +
                    '<div class="title">' +
                        '<span>'+settings.title+'</span>' +
                    '</div>' +
                    '<i class="toggle"></i>' +
                    '</div>').appendTo(widget).click(function(){ collapse(widget,$content.is(":hidden"));});
                $content.appendTo(widget);
                if(settings.height)$content.height(settings.height);
                if(settings.width){
                    $this.width(settings.width);
                    $content.width(settings.width);
                }
                if(settings.style)$content.css(style);
                settings.background=$this.css("background-image");
                $this.find(".toggle").click(function(event){
                    Ladder.stopPropagation(event);
                    collapse(widget,$content.is(":hidden"));
                });
            }
        });
        return {
            widgets:widgets,
            collapse:collapse
        }
    }
    return Ladder;
};
	if(typeof define === "function" && define.amd){
		require.config({
		    paths: { "core":'ladder-core'}
		});
		define(['core'],__functionForUi);
	}else{
		__functionForUi();
	}
})();

	

